var url = 'http://47.108.197.28:3000/search?keywords=';
var search = location.search.slice(location.search.indexOf("?")+1);
search = decodeURIComponent(search);
console.log(search);
if(search){
    httpUrl = url + search;
    $("#app").val(search);
    var xhr = new XMLHttpRequest();
    xhr.open("GET",httpUrl,true);
    xhr.send();
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4){
            if(xhr.status >= 200 && xhr.status<300){
                
                var res = JSON.parse(xhr.responseText)
                console.log(res);
                res.result.songs.forEach(item=>{
                var {name,artists,album,duration} = item;
                var artistName;
                var id;
                artists.forEach(item=>{
                    artistName = item.name;
                    id = item.id;
                    console.log(id);
                })
                album = album.name;
                if(album.length>8){
                    album = album.slice(0,8)+"...";
                }
                duration = Math.ceil(duration/3000);
                var second = duration%60;
                if(second<10){
                    second = "0" + second;
                }
                var minute = Math.floor(duration/60);
                var time = minute+":"+second;
                
                var temple = `
                <div class="item">
                    <a href="author.html?id=${id}&search=${search}" class="song">${name}</a>
                    <p class="singer">${artistName}</p>
                    <p class="plays">《${album}》</p>
                    <p class="time">${time}</p>
                </div>
                `
                $("#songs").append(temple);
            })
            $("#result").append(`<p>搜索"${search}"，得到${res.result.songs.length}条数据</p>`)
        }}
    }
}
$("#app").keydown(function(event){
    var value = $(this).val();
    if(event.keyCode == 13 && value!=0){
        if(value.trim()){
            var httpUrl = url + value;
            var xhr = new XMLHttpRequest();
            xhr.open("GET",httpUrl,true);
            xhr.send();
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4){
                    if(xhr.status >= 200 && xhr.status<300){
                        var res = JSON.parse(xhr.responseText)
                        res.result.songs.forEach(item=>{
                        var {name,artists,album,duration} = item;
                        var artistName;
                        artists.forEach(item=>{
                            artistName = item.name;
                        })
                        album = album.name;
                        if(album.length>8){
                            album = album.slice(0,8)+"...";
                        }
                        duration = Math.ceil(duration/3000);
                        var second = duration%60;
                        if(second<10){
                            second = "0" + second;
                        }
                        var minute = Math.floor(duration/60);
                        var time = minute+":"+second;
                        
                        var temple = `
                        <div class="item">
                            <a href="author.html?id=${id}&search=${search}" class="song">${name}</a>
                            <p class="singer">${artistName}</p>
                            <p class="plays">《${album}》</p>
                            <p class="time">${time}</p>
                        </div>
                        `
                        $("#songs").append(temple);
                    })
                    $("#result").append(`<p>搜索"${value}"，得到${res.result.songs.length}条数据</p>`)
                }}
            }
        }
        $(this).val("");
    }
})

